﻿Imports Global_Visas_Business_Layer
Imports System.Text
Imports System.Data
Imports System.ComponentModel
Imports CRMCONTROLLIBRARY
Partial Public Class frmAddAccount
    Public Event ShowDynamicTab(ByVal userControlName As String)
    Public Event CloseDynamicTab(ByVal userControlName As String)
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Try
            RaiseEvent CloseDynamicTab("addaccount")
        Catch exc As Exception
            CommonFuncs.ErrorHandler(exc, "frmAddAcount.btnClose_Click")
        End Try
    End Sub

    Private Sub chkAddContact_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Me.grpContact.IsEnabled = Me.chkAddContact.IsChecked
        Catch exc As Exception
            CommonFuncs.ErrorHandler(exc, "frmAddAcount.chkAddContact_CheckedChanged")
        End Try
    End Sub

    Private Sub chkAddCandidate_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Me.grpCandidate.IsEnabled = Me.chkAddCandidate.IsChecked
        Catch exc As Exception
            CommonFuncs.ErrorHandler(exc, "frmAddAcount.chkAddCandidate_CheckedChanged")
        End Try
    End Sub

    Private Sub CorpPrivToggle(ByVal sender As Object, ByVal e As System.EventArgs)

        If Me.radAddCompany.IsChecked Then
            Me.grpCompany.IsEnabled = True
            Me.grpCandidate.IsEnabled = True
            Me.grpContact.IsEnabled = True
            'Me.chkAddCandidate.IsEnabled = True
            Me.chkAddContact.IsEnabled = True
            Me.chkAddContact.IsChecked = True
            'TabControl1.SelectTab("TabPage1")
            TabControl1.SelectedItem = TabItem1
        ElseIf Me.radAddPrivate.IsChecked Then
            Me.grpCompany.IsEnabled = False
            Me.grpContact.IsEnabled = False
            Me.grpCandidate.IsEnabled = True
            Me.chkAddContact.IsEnabled = False
            Me.chkAddContact.IsChecked = False
            Me.chkAddCandidate.IsEnabled = False
            Me.chkAddCandidate.IsChecked = True
            'TabControl1.SelectTab("TabPage2")
            TabControl1.SelectedItem = tabItem2
        End If

    End Sub

    Private Function ValidateForm() As Boolean
        Try


            Dim Exp, Ret As Integer

            If Me.drpCaseProduct.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select product", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Function
            End If

            If Me.drpCaseWhereHear.Text.Trim = String.Empty Then
                If MsgBox("How the person heard about Global Visas could not be added to the database. Do you wish to continue?", MsgBoxStyle.YesNo, "Error") = MsgBoxResult.No Then
                    Me.drpCaseWhereHear.Focus()
                    Exit Function
                End If
                'MsgBox("Please select where heard", MsgBoxStyle.Exclamation, "Error")
                'Exit Function
            End If

            If Me.drpCaseCon.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("Please select a consultant", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Function
            End If

            If Me.radAddCompany.IsChecked Then
                'check company stuff
                If Me.txtCompName.Text.Trim = String.Empty Then
                    CommonFuncs.DisplayMessage("You must enter a company name", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.txtCompName.Focus()
                    Exit Function
                End If

                If Me.drpIndustry.SelectedIndex = -1 Then
                    CommonFuncs.DisplayMessage("You must select an Industry for the company", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpIndustry.Focus()
                    Exit Function
                End If

                If Me.txtCompName.Text.Length > 50 Then
                    CommonFuncs.DisplayMessage("Company name cannot be more than 50 characters, please revise", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.txtCompName.Focus()
                    Exit Function
                End If

                If Me.drpSize.SelectedIndex = -1 Then
                    CommonFuncs.DisplayMessage("You must enter a company size", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpSize.Focus()
                    Exit Function
                End If

                If Me.drpStatus.SelectedIndex = -1 Then
                    CommonFuncs.DisplayMessage("Please select a status for the company", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpStatus.Focus()
                    Exit Function
                End If

                If Me.chkAddCompAddress.IsChecked Then

                    If Me.Address1.Description.SelectedIndex = -1 Then
                        CommonFuncs.DisplayMessage("You must enter a valid description for the company address (or unselect)", CommonDialog.CustomDialogIcons.Information, "Error")
                        Me.Address1.Description.Focus()
                        Exit Function
                    End If

                    If Me.Address1.AddressText.Text.Trim = String.Empty Then
                        CommonFuncs.DisplayMessage("You must enter a valid company address (or unselect)", CommonDialog.CustomDialogIcons.Information, "Error")
                        'Me.Address1.txtAddress.Focus()
                        Me.Address1.AddressText.Focus()
                        Exit Function
                    End If

                End If

            End If

            If Me.chkAddContact.IsChecked Then
                'check contact details

                If Me.txtContactFName.Text.Trim = String.Empty Or Me.txtContactLName.Text.Trim = String.Empty Then
                    CommonFuncs.DisplayMessage("You must enter a valid name for the contact", CommonDialog.CustomDialogIcons.Information, "Error")
                    If Me.txtContactFName.Text.Trim = String.Empty Then
                        Me.txtContactFName.Focus()
                    Else
                        Me.txtContactLName.Focus()
                    End If
                    Exit Function
                End If

                If Me.drpContactRole.SelectedIndex = -1 Then
                    CommonFuncs.DisplayMessage("You must select a role for the contact", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpContactRole.Focus()
                    Exit Function
                End If

                If Me.radConAddAdd.IsChecked = 2 Then
                    'contact address

                    If Me.Address2.Description.SelectedIndex = -1 Then
                        CommonFuncs.DisplayMessage("You must enter a valid description for the contact address (or unselect)", CommonDialog.CustomDialogIcons.Information, "Error")
                        Me.Address2.Description.Focus()
                        Exit Function
                    End If

                    If Me.Address2.AddressText.Text.Trim = String.Empty Then
                        CommonFuncs.DisplayMessage("You must enter a valid contact address (or unselect)", CommonDialog.CustomDialogIcons.Information, "Error")
                        Me.Address2.Description.Focus()
                        Exit Function
                    End If


                End If

            End If

            If Me.chkAddCandidate.IsChecked Then
                'check candidate
                If Me.txtCandFName.Text.Trim = String.Empty Or Me.txtCandLName.Text.Trim = String.Empty Then
                    CommonFuncs.DisplayMessage("You must enter a valid name for the candidate", CommonDialog.CustomDialogIcons.Information, "Error")
                    If Me.txtCandFName.Text.Trim = String.Empty Then
                        Me.txtCandFName.Focus()
                    Else
                        Me.txtCandLName.Focus()
                    End If
                    Exit Function
                End If

                If Me.drpNat.SelectedIndex = -1 Then
                    Call CommonFuncs.DisplayMessage("You must enter a nationality for the candidate", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpNat.Focus()
                    Exit Function
                End If

                If Me.drpRes.SelectedIndex = -1 Then
                    CommonFuncs.DisplayMessage("You must enter the country of residence for the candidate", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpRes.Focus()
                    Exit Function
                End If

                If Me.drpUKVisa.SelectedIndex = -1 Then
                    CommonFuncs.DisplayMessage("You must enter a UK visa for the candidate (or select 'none')", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.drpUKVisa.Focus()
                    Exit Function
                End If

                'check Expiry Date
                'If Not IsDate(Me.txtUKVisaExp.Text.Trim) Then
                '    MsgBox("Please enter a valid Expiry Date for the candidate", MsgBoxStyle.Exclamation, "Error")
                '    Me.txtUKVisaExp.Focus()
                '    Exit Function
                'End If

                If Me.txtUKVisaExp.Text.Trim = String.Empty Then
                    Exp = 0
                Else
                    Exp = 1
                End If

                Ret = CommonDataFunctions.GblFunc_Validate_NatResVisa(Me.drpNat.SelectedValue, Me.drpRes.SelectedValue, Me.drpUKVisa.SelectedValue, Exp, 1)

                If Ret >= 1 And Ret <= 3 Then
                    Exit Function
                End If

                If Ret = 10 Then
                    CommonFuncs.DisplayMessage("Error validating candidate", CommonDialog.CustomDialogIcons.Information, "Error")
                    Exit Function
                End If

                'check dob
                'If Not IsDate(Me.txtDOB.Text.Trim) Then
                '    MsgBox("Please enter a valid Date of Birth for the candidate", MsgBoxStyle.Exclamation, "Error")
                '    Me.txtDOB.Focus()
                '    Exit Function
                'End If

                'check passport expiry date
                If Not IsDate(Me.txtPassportExp.Text.Trim) And Me.txtPassportExp.Text.Trim <> "" Then
                    CommonFuncs.DisplayMessage("Please enter a valid passport expiry date for the candidate, or leave blank", CommonDialog.CustomDialogIcons.Information, "Error")
                    Me.txtPassportExp.Focus()
                    Exit Function
                End If

                If Me.chkAddCandAddress.IsChecked Then
                    'candidate address

                    If Me.Address3.Description.SelectedIndex = -1 Then
                        CommonFuncs.DisplayMessage("You must enter a valid description for the candidate address (or unselect)", CommonDialog.CustomDialogIcons.Information, "Error")
                        Me.Address3.Description.Focus()
                        Exit Function
                    End If

                    If Me.Address3.AddressText.Text.Trim = String.Empty Then
                        CommonFuncs.DisplayMessage("You must enter a valid candidate address (or unselect)", CommonDialog.CustomDialogIcons.Information, "Error")
                        Me.Address3.AddressText.Focus()
                        Exit Function
                    End If

                End If
            End If

            Return True
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAddAccount : ValidateForm")
        Finally

        End Try
    End Function

    Private Function SaveAccount() As Boolean
        Dim TasksEvents As New TasksEvents
        Dim SQL As StringBuilder
        Dim CompanyId As Long
        Dim ContactId As Long
        Dim CandidateId As Long
        Dim CaseId As Long
        Dim Msg As String
        Dim bFail As Boolean = True
        Dim cmn As New CommonFuncs
        ' check validation


        If Not Me.ValidateForm Then
            Exit Function
        End If

        Try
            CommonFuncs.BeginTrans()

            'All required fields check out, start to add records

            If Me.radAddCompany.IsChecked Then

                'add a company

                SQL = New StringBuilder

                With SQL
                    .Append("Insert Into D_Company (Company_Name, Company_IndustryID,Company_sizeID, Company_EnteredBy, Company_DTEntered, Company_AccountManagerID, company_caseworkerid, Company_StatusID, company_website)")
                    .Append(" VALUES('" & cmn.SwapQuotes(Me.txtCompName.Text.Trim) & "'," & CommonFuncs.DropDownId(Me.drpIndustry) & ",")
                    .Append(CommonFuncs.DropDownId(Me.drpSize) & ",'" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "',")
                    .Append(GlobalCRM.CurrentConID & ",11," & CommonFuncs.DropDownId(Me.drpStatus) & ",'")
                    .Append(cmn.SwapQuotes(Me.txtWebsite.Text.Trim) & "')")
                End With

                'If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                'CommonFuncs.RollbackTrans()
                'Exit Function
                'End If

                CompanyId = CommonDataFunctions.ExecuteSQL(SQL.ToString, True)

                GlobalCRM.CurrentCompanyID = CompanyId
                If CompanyId = 0 Then
                    CommonFuncs.RollbackTrans()
                    Exit Function
                End If

                SQL = New StringBuilder
                With SQL
                    .Append("Insert Into D_Advertising")
                    .Append(" (Advertising_WhereHear , Advertising_EnteredBy, Advertising_LinkID, Advertising_LinkType)")
                    .Append("VALUES('" & cmn.SwapQuotes(Me.drpCaseWhereHear.Text) & "','" & GlobalCRM.CurrentConName & "'," & CompanyId & ",1) ")
                End With

                If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                    CommonFuncs.RollbackTrans()
                    Exit Function
                End If

                If Me.chkAddCompAddress.IsChecked Then

                    Dim CompanyAddressId As Long

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_Address (Address_Text, Address_Region, Address_Country, Address_Postcode, Address_DTEntered, Address_EnteredBy)")
                        .Append(" VALUES('" & cmn.SwapQuotes(Me.Address1.AddressText.Text.Trim) & "','")

                        If Me.Address1.CountryComboBox.SelectedValue = GlobalCRM.COUNTRY_UK Then 'UK
                            .Append(cmn.SwapQuotes(Me.Address1.CountryComboBox.Text))
                        Else
                            .Append(cmn.SwapQuotes(Me.Address1.RegionText.Text.Trim))
                        End If

                        .Append("','" & cmn.SwapQuotes(Me.Address1.CountryComboBox.Text.Trim) & "','")
                        .Append(cmn.SwapQuotes(Me.Address1.PostcodeText.Text.Trim) & "', '")
                        .Append(cmn.FormatDate(Now, True) & "','" & GlobalCRM.CurrentConName & "' )")
                    End With

                    'If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                    '    CommonFuncs.RollbackTrans()
                    '    Exit Function
                    'End If

                    CompanyAddressId = CommonDataFunctions.ExecuteSQL(SQL.ToString, True)

                    If CompanyAddressId = 0 Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_AddressLink (AddressLink_CompanyId, AddressLink_AddressID, AddressLink_EnteredBy, AddressLink_DTEntered, AddressLink_Name, AddressLink_Type, AddressLink_Owner)")
                        .Append(" VALUES(" & CompanyId & "," & CompanyAddressId & ",'" & GlobalCRM.CurrentConName & "','")
                        .Append(cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.Address1.Description.Text.Trim) & "',3,1)")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If
                End If

            End If

            If Me.chkAddContact.IsChecked Then
                'add a contact

                Dim ContactAddressId As Long

                SQL = New StringBuilder

                With SQL
                    .Append("Insert Into D_Person (Person_FName, Person_LName, Person_Title, Person_EnteredBy, Person_DTEntered, Person_NatID, Person_ResidentID, Person_UKVIsaID, Person_CompanyID, Person_CompanyRoleID)")
                    .Append(" VALUES('" & cmn.SwapQuotes(Me.txtContactFName.Text.Trim) & "','" & cmn.SwapQuotes(Me.txtContactLName.Text.Trim) & "','")
                    .Append(Me.drpContactTitle.Text.Trim & "','" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "',71,71,1, " & CompanyId & ", " & Me.drpContactRole.SelectedValue & ")")
                End With

                'If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                '    CommonFuncs.RollbackTrans()
                '    Exit Function
                'End If

                ContactId = CommonDataFunctions.ExecuteSQL(SQL.ToString, True)

                If ContactId = 0 Then
                    CommonFuncs.RollbackTrans()
                    Exit Function
                End If


                If Me.radConAddNone.IsChecked = False Then
                    ' add Contact address 

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_Address (Address_Text, Address_Region, Address_Country, Address_Postcode, Address_DTEntered, Address_EnteredBy )")
                        .Append(" VALUES('" & cmn.SwapQuotes(Me.Address2.AddressText.Text.Trim) & "','")

                        If Me.Address2.CountryComboBox.SelectedValue = GlobalCRM.COUNTRY_UK Then 'UK
                            .Append(cmn.SwapQuotes(Me.Address2.CountyComboBox.Text.Trim))

                        Else
                            .Append(cmn.SwapQuotes(Me.Address2.RegionText.Text.Trim))
                        End If

                        .Append("','" & cmn.SwapQuotes(Me.Address2.CountryComboBox.Text.Trim) & "','")
                        .Append(cmn.SwapQuotes(Me.Address2.PostcodeText.Text.Trim) & "', '" & cmn.FormatDate(Now, True) & "','" & GlobalCRM.CurrentConName & "' )")
                    End With

                    'If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                    '   commonfuncs.RollbackTrans()
                    '    Exit Function
                    'End If

                    ContactAddressId = CommonDataFunctions.ExecuteSQL(SQL.ToString, True)

                    If ContactAddressId = 0 Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                    ' add one address link for the contact
                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_AddressLink (AddressLink_personId, AddressLink_AddressID, AddressLink_EnteredBy, AddressLink_DTEntered, AddressLink_Name, AddressLink_Type, AddressLink_Owner)")
                        .Append(" VALUES(" & ContactId & "," & ContactAddressId & ",'" & GlobalCRM.CurrentConName & "','")
                        .Append(cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.Address2.Description.Text.Trim) & "',3,1)")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                    If Me.radConAddCo.IsChecked = True Then
                        ' add one additional address link for the company (to the contacts address)
                        SQL = New StringBuilder

                        With SQL
                            .Append("Insert into D_AddressLink (AddressLink_CompanyId, AddressLink_AddressID, AddressLink_EnteredBy, AddressLink_DTEntered, AddressLink_Name, AddressLink_Type, AddressLink_Owner)")
                            .Append(" VALUES(" & CompanyId & "," & ContactAddressId & ",'" & GlobalCRM.CurrentConName & "','")
                            .Append(cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.Address3.Description.Text.Trim) & "',3,0)")
                        End With

                        If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                            CommonFuncs.RollbackTrans()
                            Exit Function
                        End If

                    End If


                End If

                'If Me.chkAddCandidate.ischecked Then

                'End If

                If Me.txtContactEmail.Text.Trim <> "" Then
                    'insert an e-mail address

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_EmailAddress (EMailAddress_EnteredBy, EMailAddress_DTEntered, EMailAddress_Address,EMailAddress_PersonID, EMailAddress_Type, EMailAddress_Name)")
                        .Append(" VALUES('" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.txtContactEmail.Text.Trim) & "'," & ContactId & ",1,'Main' )")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If

                If Me.txtContactPhone.Text.Trim <> "" Then
                    'insert a phone no

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_PhoneNumber (PhoneNumber_EnteredBy, PhoneNumber_DTEntered, PhoneNumber_number, PhoneNumber_PersonID, PhoneNumber_Type, PhoneNumber_Name)")
                        .Append(" VALUES('" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "','")
                        .Append(cmn.SwapQuotes(Me.txtContactPhone.Text.Trim) & "'," & ContactId & ",1,'Phone' )")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If

                If Me.txtContactFax.Text.Trim <> "" Then

                    'insert a fax no
                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_PhoneNumber (PhoneNumber_EnteredBy, PhoneNumber_DTEntered, PhoneNumber_number, PhoneNumber_PersonID, PhoneNumber_Type, PhoneNumber_Name)")
                        .Append(" VALUES('" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.txtContactFax.Text.Trim) & "'," & ContactId & ",2,'Fax' )")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If

            End If

            If Me.chkAddCandidate.IsChecked Then
                'add a candidate

                SQL = New StringBuilder

                With SQL

                    .Append("Insert Into D_Person (Person_FName, Person_LName, Person_Title, Person_EnteredBy, Person_DTEntered, Person_NatID, Person_ResidentID, Person_UKVisaID, Person_JobTitle, Person_PassportNo, Person_Qualification, Person_CompanyID, Person_CompanyRoleID, Person_DoB, Person_ExpiryDt, Person_PassportDTExpiry)")
                    .Append(" VALUES('" & cmn.SwapQuotes(Me.txtCandFName.Text.Trim) & "','" & cmn.SwapQuotes(Me.txtCandLName.Text.Trim) & "','")
                    .Append(cmn.SwapQuotes(Me.drpCandTitle.Text) & "','" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now) & "',")
                    .Append(Me.drpNat.SelectedValue & "," & Me.drpRes.SelectedValue & "," & Me.drpUKVisa.SelectedValue & ",'")
                    .Append(cmn.SwapQuotes(Me.txtJobTitle.Text.Trim) & "','" & cmn.SwapQuotes(Me.txtPassportNo.Text.Trim) & "','")
                    .Append(cmn.SwapQuotes(Me.drpQual.Text) & "'," & CompanyId)
                    If CompanyId = 0 Then
                        .Append(", 0,")
                    Else
                        .Append(", 1,")
                    End If

                    If Not IsDate(Me.txtDOB.Text.Trim) Then
                        .Append("Null, ")
                    Else
                        .Append("'" & cmn.FormatDate(Me.txtDOB.Text.Trim) & "',")
                    End If

                    If Not IsDate(Me.txtUKVisaExp.Text.Trim) Then
                        .Append("Null ,")
                    Else
                        .Append("'" & cmn.FormatDate(Me.txtUKVisaExp.Text.Trim) & "',")
                    End If

                    If Not IsDate(Me.txtPassportExp.Text.Trim) Then
                        .Append("Null)")
                    Else
                        .Append("'" & cmn.FormatDate(Me.txtPassportExp.Text.Trim) & "')")
                    End If

                End With

                If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                    CommonFuncs.RollbackTrans()
                    Exit Function
                End If

                CandidateId = CommonDataFunctions.ExecuteSQL(SQL.ToString, True)
                'CandidateId = DB.GetIdentity

                GlobalCRM.CurrentCandidateID = CandidateId

                If CandidateId = 0 Then
                    CommonFuncs.RollbackTrans()
                    Exit Function
                End If

                If CompanyId = 0 Then
                    'Private individual add where hear here.

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert Into D_Advertising")
                        .Append(" (Advertising_WhereHear , Advertising_EnteredBy, Advertising_LinkID, Advertising_LinkType)")
                        .Append("VALUES('" & Me.drpCaseWhereHear.Text & "','" & GlobalCRM.CurrentConName & "'," & CandidateId & ",0) ")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If

                If Me.radAddPrivate.IsChecked Then
                    ContactId = 0
                End If

                If Me.chkAddCandAddress.IsChecked Then

                    Dim CandidateAddressId As Long

                    SQL = New StringBuilder
                    With SQL
                        .Append("Insert into D_Address ( Address_Text, Address_Region, Address_Country, Address_Postcode, Address_DTEntered, Address_EnteredBy )")
                        .Append(" VALUES('" & cmn.SwapQuotes(Me.Address3.AddressText.Text.Trim) & "','")

                        If Me.Address3.CountryComboBox.SelectedValue = GlobalCRM.COUNTRY_UK Then 'UK
                            .Append(cmn.SwapQuotes(Me.Address3.CountyComboBox.Text.Trim))
                        Else
                            .Append(cmn.SwapQuotes(Me.Address3.RegionText.Text.Trim))
                        End If

                        .Append("','" & cmn.SwapQuotes(Me.Address3.CountryComboBox.Text) & "','" & cmn.SwapQuotes(Me.Address3.PostcodeText.Text.Trim) & "','")
                        .Append(cmn.FormatDate(Now, True) & "','" & GlobalCRM.CurrentConName & "' )")

                    End With

                    'If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                    '    CommonFuncs.RollbackTrans()
                    '    Exit Function
                    'End If
                    CandidateAddressId = CommonDataFunctions.ExecuteSQL(SQL.ToString, True)
                    'CandidateAddressId = DB.GetIdentity

                    If CandidateAddressId = 0 Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_AddressLink (AddressLink_personId, AddressLink_AddressID, AddressLink_EnteredBy, AddressLink_DTEntered, AddressLink_Name, AddressLink_Type, AddressLink_Owner)")
                        .Append(" VALUES(" & CandidateId & "," & CandidateAddressId & ",'" & GlobalCRM.CurrentConName & "','")
                        .Append(cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.Address3.Description.Text.Trim) & "',3,1)")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If

                If Me.txtCandEmail.Text.Trim <> "" Then
                    'insert an e-mail address

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_EmailAddress (EMailAddress_EnteredBy, EMailAddress_DTEntered, EMailAddress_Address,EMailAddress_PersonID, EMailAddress_Type, EMailAddress_Name)")
                        .Append(" VALUES('" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now) & "','" & cmn.SwapQuotes(Me.txtCandEmail.Text.Trim) & "'," & CandidateId & ",1,'Main' )")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If

                If Me.txtCandPhone.Text.Trim <> "" Then
                    'insert a phone no

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_PhoneNumber (PhoneNumber_EnteredBy, PhoneNumber_DTEntered, PhoneNumber_number, PhoneNumber_PersonID, PhoneNumber_Type, PhoneNumber_Name)")
                        .Append(" VALUES('" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.txtCandPhone.Text.Trim) & "'," & CandidateId & ",1,'Phone' )")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If

                If Me.txtCandFax.Text.Trim <> "" Then

                    'insert a fax no

                    SQL = New StringBuilder

                    With SQL
                        .Append("Insert into D_PhoneNumber (PhoneNumber_EnteredBy, PhoneNumber_DTEntered, PhoneNumber_number, PhoneNumber_PersonID, PhoneNumber_Type, PhoneNumber_Name)")
                        .Append(" VALUES('" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "','" & cmn.SwapQuotes(Me.txtCandFax.Text.Trim) & "'," & CandidateId & ",2,'Fax' )")
                    End With

                    If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                        CommonFuncs.RollbackTrans()
                        Exit Function
                    End If

                End If
            End If

            'All the other records are now done, put in the Case, linked to the above.

            SQL = New StringBuilder

            With SQL
                .Append("Insert into D_Case (Case_ConSaleID, Case_CompanyID, Case_ContactID, Case_CandidateID, Case_ProductID, Case_StatusID, Case_DTEntered, Case_EnteredBy, Case_DTStatus)")
                .Append(" VALUES(" & Me.drpCaseCon.SelectedValue & "," & CompanyId & "," & ContactId & "," & CandidateId & "," & Me.drpCaseProduct.SelectedValue & ", 10,'" & cmn.FormatDate(Now, True) & "','" & GlobalCRM.CurrentConName & "','" & cmn.FormatDate(Now, True) & "')")
            End With

            'If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
            '    CommonFuncs.RollbackTrans()
            '    Exit Function
            'End If

            'Added Case, get ID

            ' CaseId = DB.GetIdentity
            CaseId = CommonDataFunctions.ExecuteSQL(SQL.ToString, True)

            If CaseId = 0 Then
                CommonFuncs.RollbackTrans()
                Exit Function
            End If

            'Got Case ID, add related records for product

            If CompanyId <> 0 Then
                CandidateId = 0
            End If

            'Add charges

            SQL = New StringBuilder

            With SQL
                .Append("INSERT INTO A_Charges (Charge_Type, Charge_VAT, Charge_Amount, Charge_TPCost, Charge_DTEntered, Charge_EnteredBy, Charge_CompanyID, Charge_PersonID, Charge_CaseID, Charge_Desc, Charge_Category )")
                .Append(" SELECT DefCharge_Type, DefCharge_VAT, DefCharge_Amount, DefCharge_TPCost, '" & cmn.FormatDate(Now, True) & "' AS now, '" & GlobalCRM.CurrentConName & "' AS Cons, " & CompanyId & " AS CoID, " & CandidateId & " AS PerID, " & CaseId & " AS LeadID, 'Default Charge' AS Description, DefCharge_Category")
                .Append(" FROM D_DefCharges")
                .Append(" WHERE DefCharge_ProductID = " & Me.drpCaseProduct.SelectedValue)
            End With

            If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                CommonFuncs.RollbackTrans()
                Exit Function
            End If

            'Add Def Doc Reqs

            'Add Document requirements
            SQL = New StringBuilder

            With SQL
                .Append("INSERT INTO D_DocReqs")
                .Append(" (DocReq_Name, DocReq_Type, DocReq_TypeID, DocReq_DTEntered, DocReq_EnteredBy, DocReq_CaseID)")
                .Append(" SELECT DefDocReq_Name, DefDocReq_Type, DefDocReq_TypeID ,GETDATE() AS Entered, '" & GlobalCRM.CurrentConName & "' AS enteredBy, " & CaseId & " AS CaseID")
                .Append(" FROM D_DefDocReqs")
                .Append(" Left Outer Join L_DocReqTypes on D_DefDocReqs.DefDocReq_TypeID = DocReqType_ID")
                .Append(" WHERE (DefDocReq_ProductID = " & Me.drpCaseProduct.SelectedValue & ")")
                .Append(" AND (DocReqType_ClientType IN ")
                If CompanyId = 0 Then
                    .Append(" (0,1))")
                Else
                    .Append(" (0,2))")
                End If
            End With

            If Not CommonDataFunctions.ExecuteSQL(SQL.ToString) Then
                CommonFuncs.RollbackTrans()
                Exit Function
            End If

            Msg = Me.drpCaseProduct.Text.ToString & " case added with the Cold Call Sheet"

            If CompanyId = 0 Then
                TasksEvents.GblFunc_Add_Event("Case Added", Msg, TasksEvents.EventStatus.SingleEvent, CandidateId, 2)
            Else
                TasksEvents.GblFunc_Add_Event("Case Added", Msg, TasksEvents.EventStatus.SingleEvent, CompanyId, 3)
            End If

            If Me.txtCaseNotes.Text.Trim <> "" Then
                TasksEvents.GblFunc_Add_Event("Initial Case Notes", cmn.SwapQuotes(Me.txtCaseNotes.Text.Trim), TasksEvents.EventStatus.SingleEvent, CaseId, 1)
            End If

            If Me.radAddCompany.IsChecked Then
                TasksEvents.GblFunc_Add_SysEvent("Activity", "New company added with CCS (" & CompanyId & ")")
            ElseIf Me.radAddPrivate.IsChecked Then
                TasksEvents.GblFunc_Add_SysEvent("Activity", "New private individual added with CCS (" & ContactId & ")")
            End If

            CommonFuncs.CommitTrans()
            bFail = False

            Return True
        Catch ex As Exception
            CommonFuncs.DisplayMessage("There was an error Saving the account." & vbCrLf & "Please retry then ask support if no success.", CommonDialog.CustomDialogIcons.Information, "Assign Case Worker")
            If CommonFuncs.IsTransactionOpen = True Then
                CommonFuncs.RollbackTrans()
            End If
            CommonFuncs.ErrorHandler(ex, "frmAddAccount.SaveAccount", True)
        Finally
            If bFail = True Then
                CommonFuncs.DisplayMessage("There was an error saving the account." & vbCrLf & "Please retry then ask support if no success.", CommonDialog.CustomDialogIcons.Information, "Assign Case Worker")
            End If
        End Try
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If drpCaseProduct.Text = String.Empty Or drpCaseType.Text = String.Empty Then
                CommonFuncs.DisplayMessage("You must select a Product and a Product Type before you can save the record.", CommonDialog.CustomDialogIcons.Information, String.Empty)
                Exit Sub
            Else
                If Not Me.SaveAccount Then

                Else
                    If radAddCompany.IsChecked Then
                        'GlobalCRM.CurrentCompanyID = CompanyId
                        GlobalCRM.CurrentSearchType = GlobalCRM.enuSearchType.eCompany
                        RaiseEvent CloseDynamicTab("addaccount")
                        'frmMainDisplay.MdiParent = frmMDIMain
                        'frmMainDisplay.Show()
                    End If
                    If radAddPrivate.IsChecked Then
                        RaiseEvent CloseDynamicTab("addaccount")
                        'frmMainDisplay.MdiParent = frmMDIMain
                        'frmMainDisplay.Show()
                        'frmMainDisplay.TabControl1.SelectedTab = frmMainDisplay.tabPeople
                    End If

                End If
            End If

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmMainForm.btnSave_Click", False)
        End Try

    End Sub

    Private Sub AddAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        With Me.drpBranch
            .DisplayMemberPath = "Branch_Name"
            .SelectedValuePath = "Branch_ID"
            .ItemsSource = CType(CommonDataFunctions.BranchList, IListSource).GetList
        End With

        Me.drpBranch.SelectedValue = CommonDataFunctions.GetLoggedOnConsultantBranchID
        With Me.drpIndustry
            .DisplayMemberPath = "Industry"
            .SelectedValuePath = "industry_id"
            .ItemsSource = CType(Admin.IndustryList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpSize
            .DisplayMemberPath = "description"
            .SelectedValuePath = "companysize_id"
            .ItemsSource = CType(CommonDataFunctions.CompanySizeList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpStatus
            .DisplayMemberPath = "companystatus_name"
            .SelectedValuePath = "company_statusid"
            .ItemsSource = CType(CommonDataFunctions.CompanyStatusList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpContactTitle
            .DisplayMemberPath = "title_text"
            .SelectedValuePath = "title_id"
            .ItemsSource = CType(CommonDataFunctions.TitleList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpContactRole
            .DisplayMemberPath = "role_name"
            .SelectedValuePath = "role_id"
            .ItemsSource = CType(Company.CompanyRoleList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpCandTitle
            .DisplayMemberPath = "title_text"
            .SelectedValuePath = "title_id"
            .ItemsSource = CType(CommonDataFunctions.TitleList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpQual
            .DisplayMemberPath = "qual_name"
            .SelectedValuePath = "qual_id"
            .ItemsSource = CType(CommonDataFunctions.QualificationList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpNat
            .DisplayMemberPath = "country_nationality"
            .SelectedValuePath = "country_id"
            .ItemsSource = CType(CommonDataFunctions.NationalityList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpRes
            .DisplayMemberPath = "country_name"
            .SelectedValuePath = "country_id"
            .ItemsSource = CType(CommonDataFunctions.CountryList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpUKVisa
            .DisplayMemberPath = "ukvisa_name"
            .SelectedValuePath = "ukvisa_id"
            .ItemsSource = CType(CommonDataFunctions.UKVisaList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpCaseCon
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList, IListSource).GetList
            .SelectedIndex = -1
        End With


        With Me.drpCaseType
            .DisplayMemberPath = "Product Type"
            .SelectedValuePath = "ProductType_ID"
            .ItemsSource = CType(Products.ProductTypesList(), IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpCaseWhereHear
            .DisplayMemberPath = "wherehear_text"
            .SelectedValuePath = "wherehear_id"
            .ItemsSource = CType(CommonDataFunctions.WhereHearList, IListSource).GetList
            .SelectedIndex = -1
        End With

        ' select current consultant in consultant drop down menu
        drpCaseCon.Text = GlobalCRM.CurrentConName

        ' populate the Country and Country drop down lists on the Address user controls
        ' xxx - ideally this should be done in the user control
        With Me.Address1.CountyComboBox
            .DisplayMemberPath = "county_name"
            .SelectedValuePath = "county_id"
            .ItemsSource = CType(CommonDataFunctions.CountyList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.Address1.CountryComboBox
            .DisplayMemberPath = "country_name"
            .SelectedValuePath = "country_id"
            .ItemsSource = CType(CommonDataFunctions.CountryList, IListSource).GetList
            .SelectedValue = 71 'GlobalCRM.COUNTRY_UK
        End With

        With Me.Address2.CountyComboBox
            .DisplayMemberPath = "county_name"
            .SelectedValuePath = "county_id"
            .ItemsSource = CType(CommonDataFunctions.CountyList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.Address2.CountryComboBox
            .DisplayMemberPath = "country_name"
            .SelectedValuePath = "country_id"
            .ItemsSource = CType(CommonDataFunctions.CountryList, IListSource).GetList
            .SelectedValue = 71 'GlobalCRM.COUNTRY_UK
        End With

        With Me.Address3.CountyComboBox
            .DisplayMemberPath = "county_name"
            .SelectedValuePath = "county_id"
            .ItemsSource = CType(CommonDataFunctions.CountyList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.Address3.CountryComboBox
            .DisplayMemberPath = "country_name"
            .SelectedValuePath = "country_id"
            .ItemsSource = CType(CommonDataFunctions.CountryList, IListSource).GetList
            .SelectedValue = 71 'GlobalCRM.COUNTRY_UK
        End With

    End Sub


    Private Sub drpCaseType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        With Me.drpCaseProduct
            .DisplayMemberPath = "product_name"
            .SelectedValuePath = "product_id"
            .ItemsSource = CType(Products.ProductListByType(Me.drpCaseType.SelectedValue, -1), IListSource).GetList
            .SelectedIndex = -1
        End With

    End Sub

    Private Sub radConAddAdd_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub radConAddCo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub radConAddNone_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If radConAddNone.IsChecked Then
            Address2.IsEnabled = False
        Else
            Address2.IsEnabled = True
        End If
    End Sub

    Private Sub chkAddCandAddress_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Address3.IsEnabled = chkAddCandAddress.IsChecked
    End Sub

    Private Sub btnCalendarDoB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' open the calendar popup form and handle the selected date
        Me.txtDOB.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtDOB.Text)
    End Sub

    Private Sub btnCalendarUKVisaExp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' open the calendar popup form and handle the selected date
        Me.txtUKVisaExp.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtUKVisaExp.Text)
    End Sub

    Private Sub btnCalendarPassportExp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' open the calendar popup form and handle the selected date
        Me.txtPassportExp.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtPassportExp.Text)
    End Sub

    Private Sub radAddPrivate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radAddPrivate.Checked
        CorpPrivToggle(sender, e)
    End Sub

    Private Sub radAddCompany_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radAddCompany.Checked
        CorpPrivToggle(sender, e)
    End Sub
    Private Sub radAddPrivate_UnChecked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radAddPrivate.Unchecked
        CorpPrivToggle(sender, e)
    End Sub

    Private Sub radAddCompany_UnChecked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radAddCompany.Unchecked
        CorpPrivToggle(sender, e)
    End Sub
    Private Sub btnCalendarDoB_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.txtDOB.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtDOB.Text)
    End Sub

    Private Sub btnCalendarUKVisaExp_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txtUKVisaExp.Text = CommonFuncs.GetPopUpCalendarDate(txtUKVisaExp.Text)
    End Sub

    Private Sub btnCalendarPassportExp_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.txtPassportExp.Text = CommonFuncs.GetPopUpCalendarDate(Me.txtPassportExp.Text)
    End Sub

    Private Sub drpBranch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpBranch.SelectionChanged
        'With Me.drpCaseType
        '    .DisplayMemberPath = "producttype_name"
        '    .SelectedValuePath = "producttype_id"
        '    .ItemsSource = CType(Products.ProductTypesList(), IListSource).GetList
        '    .SelectedIndex = -1
        'End With
    End Sub

    Public Sub CloseForm()

    End Sub



    Private Sub txtCandLName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles txtCandLName.KeyUp
        Dim SQL As New StringBuilder
        Dim dtbSearchResults As DataTable
        Try


            If e.Key = Key.Enter Then

                With SQL
                    .Append("Select D_Person.Person_ID as PersonID, D_Person.Person_Lname + ', ' + D_Person.Person_Fname as Person ,D_Person.person_DoB ,")
                    .Append("Country_Nationality as Nat , D_Company.Company_ID, isnull(D_Company.Company_Name,'Private') as Company,D_Company_Parent.Company_Name, D_Person_Partner.Person_Lname + ', ' + D_Person_Partner.Person_Fname as Partner, EMailAddress.EMailAddress_Address as EMail FROM D_Person LEFT OUTER JOIN D_Company ON D_Person.Person_CompanyID = D_Company.Company_ID LEFT OUTER JOIN D_Company D_Company_Parent ON D_Company.Company_ParentID = D_Company_Parent.Company_ID Left Outer join L_Country on D_Person.Person_NatID = Country_ID LEFT OUTER JOIN (SELECT     D_Dependants.Dependant_ParentID, D_Person.Person_LName, D_Person.Person_FName FROM D_Person INNER JOIN D_Dependants ON D_Person.Person_ID = D_Dependants.Dependant_ChildID WHERE (D_Dependants.Dependant_Spouse = 1)) D_Person_Partner  ON D_Person.Person_ID = D_Person_Partner.Dependant_ParentID LEFT OUTER JOIN (SELECT D_EMailAddress.EMailAddress_PersonID, D_EMailAddress.EMailAddress_Address FROM D_EMailAddress  WHERE (D_EMailAddress.EMailAddress_Type = 1)) EmailAddress  ON D_Person.Person_ID = EMailAddress.EMailAddress_PersonID ")
                    .Append("WHERE ")
                    .Append("D_Person.Person_Lname =  '" & txtCandLName.Text & "' ")
                    .Append(" Order By Person,D_Person.Person_Lname, D_Person.Person_Fname")
                End With
                Dim intRecordCount As Integer

                dtbSearchResults = CommonDataFunctions.GetSQLDatatable(SQL.ToString)
                If dtbSearchResults Is Nothing Then
                    Exit Sub
                End If
                intRecordCount = dtbSearchResults.Rows.Count
                If intRecordCount = 0 Then
                    Exit Sub
                End If
                If intRecordCount > 1000 Then
                    If MsgBox("Over 1000 matching records found. Would you like to continue adding the candidate", MsgBoxStyle.YesNo, "Search Results") = MsgBoxResult.Yes Then
                    Else
                        txtCandLName.Text = String.Empty
                    End If
                    Exit Sub
                End If
                Dim frmSearchCandidate As New frmSearchCandidate
                DataBinding.BindListView(dtbSearchResults, frmSearchCandidate.lvwSearchResults)
                frmSearchCandidate.txtCandName.Text = txtCandLName.Text
                frmSearchCandidate.Show()
                frmSearchCandidate = Nothing
            End If
        Catch ex As Exception
        Finally
            SQL = Nothing
            dtbSearchResults = Nothing
        End Try
    End Sub

    Private Sub txtCompName_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles txtCompName.KeyUp
        If e.Key = Key.Enter Then
            Dim myData As DataTable
            myData = Company.SimilarCompanies(Me.txtCompName.Text)
            If myData.Rows.Count > 0 Then
                'If MsgBox("That company may already exist on the system," & vbCrLf & " would you like to view possible matches?" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "(Selecting no will add a new company record)", vbYesNo, "Company found") = MsgBoxResult.Yes Then
                Dim frmSearchCompany As New frmSearchCompany
                DataBinding.BindListView(myData, frmSearchCompany.lvwSimilar)
                frmSearchCompany.txtCompanyName.Text = txtCompName.Text
                frmSearchCompany.Show()
                Exit Sub
                'End If
            End If
        End If
    End Sub

    Private Sub txtDOB_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtDOB.SelectedDateChanged
        Me.txtDOB.Text = (New CommonFuncs).FormatDate(txtDOB.SelectedDate)
    End Sub

    Private Sub txtPassportExp_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtPassportExp.SelectedDateChanged
        Me.txtPassportExp.Text = (New CommonFuncs).FormatDate(txtPassportExp.SelectedDate)
    End Sub

    Private Sub txtUKVisaExp_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles txtUKVisaExp.SelectedDateChanged
        Me.txtUKVisaExp.Text = (New CommonFuncs).FormatDate(txtUKVisaExp.SelectedDate)
    End Sub

    Private Sub drpCaseType_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpCaseType.SelectionChanged
        With Me.drpCaseProduct
            .DisplayMemberPath = "Product_Name"
            .SelectedValuePath = "Product_ID"
            .ItemsSource = CType(Products.ProductList(Me.drpCaseType.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub
End Class
